package top.lyjwn.todo.weinxin.api.applet;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.lyjwn.todo.weinxin.api.url.WXAppletUrl;
import top.lyjwn.todo.weinxin.applet.AppletAccessToken;
import top.lyjwn.todo.weinxin.param.WXAppletParam;
import top.lyjwn.todo.weinxin.param.WXBjnewsParam;
import top.lyjwn.todo.common.utils.http.HttpUtil;

@Deprecated
@Component
public class SendUniformMessage {

    @Autowired
    private WXAppletParam wxAppletParam;

    @Autowired
    private WXBjnewsParam wxBjnewsParam;

    @Autowired
    private WXAppletUrl wxAppletUrl;

    public SendUniformMessage() {
    }



    public String getUrl(AppletAccessToken accessToken) {
        return wxAppletUrl.getSEND_UNIFORM_MESSAGE().replace("#{ACCESS_TOKEN}", accessToken.getAccess_token());
    }


    public String sendMpTemplateMsg(AppletAccessToken accessToken,String openid, String template_id, String url, String pagepath, JSONObject data) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", openid);
        JSONObject mp_template_msg = new JSONObject();
        mp_template_msg.put("appid", wxBjnewsParam.getAppid());
        mp_template_msg.put("template_id", template_id);
        mp_template_msg.put("url", url);
        JSONObject miniprogram = new JSONObject();
        miniprogram.put("appid", wxAppletParam.getAppid());
        miniprogram.put("pagepath", pagepath);
        mp_template_msg.put("miniprogram", miniprogram);
        mp_template_msg.put("data", data);
        jsonObject.put("mp_template_msg", mp_template_msg);
        String jsonString = jsonObject.toJSONString();
        String request = HttpUtil.request(getUrl(accessToken), HttpUtil.METHOD_POST, jsonString);
        return request;
    }

}
